home *** CD-ROM | disk | FTP | other *** search
-
- #include <stdio.h>
- #include <stdlib.h>
- #define NUMELEM(a) (sizeof(a)/sizeof(a[0]))
-
- main()
- {
- int cmpia(const void *, const void *);
- int array[] = {25, 3, 22, -5, 3, 24};
- int i;
-
- qsort(&array[0], NUMELEM(array), sizeof(int), cmpia);
-
- printf("ascending integer order\n");
- for (i = 0; i < NUMELEM(array); ++i)
- printf("array[%d] = %2d\n", i, array[i]);
-
- return 0;
- }
-
- /* compare ints in ascending order */
-
- int cmpia(const void *pe1, const void *pe2)
- {
- const int *pi1 = pe1;
- const int *pi2 = pe2;
-
- if (*pi1 < *pi2)
- return -1;
- else if (*pi1 == *pi2)
- return 0;
- else
- return 1;
- }
-
- ascending integer order
- array[0] = -5
- array[1] = 3
- array[2] = 3
- array[3] = 22
- array[4] = 24
- array[5] = 25
-
-